<?php

/**
 * Class RoleModuleGroupLogic
 */
class RoleModuleGroupLogic extends Model
{
    private $_modelRoleModuleGroup = null;
    private $_customerId = null;

    public function __construct()
    {
        $this->_customerId = Session::$user['customerId'];
        Session::switchCustomer(0);

        $this->_modelRoleModuleGroup = D('RoleModuleGroup');

        Session::switchCustomer($this->_customerId);
    }

    /** --------------------------------------  增  -------------------------------------- */

    /**
     * 新建一个角色与模块的对应
     *
     * @param $data
     * @return mixed
     */
    public function addRoleModuleGroup($data)
    {
        return $this->_modelRoleModuleGroup->add($data);
    }

    /**
     * 批量添加
     *
     * @param $data
     * @return bool|string
     */
    public function addAll($data)
    {
        return $this->_modelRoleModuleGroup->addAll($data);
    }

    /** --------------------------------------  删  -------------------------------------- */

    /**
     * 根据roleId删除与该role关联的module
     * 自己操作自己的角色管理使用频繁
     *
     * @param $roleId
     * @return mixed
     */
    public function deleteByRoleId($roleId)
    {
        return $this->_modelRoleModuleGroup->where(['cid' => $this->_customerId, 'agent' => $roleId])->delete();    // 数据表中, agent 是wx_role表的id, 即roleId
    }

    /**
     * 根据where条件进行一些泛泛的删除操作
     *
     * @param $where
     * @return mixed
     */
    public function deleteWithWhere($where)
    {
        return $this->_modelRoleModuleGroup->where($where)->delete();
    }

    /** --------------------------------------  该  -------------------------------------- */

    /** --------------------------------------  查  -------------------------------------- */

    /**
     * 查询单个列
     *
     * @param array  $where
     * @param string $field
     * @param bool   $list
     * @return mixed
     */
    public function getField($where = [], $field, $list = FALSE)
    {
        if (!isset($where['cid'])) $where['cid'] = $this->_customerId;
        return $this->_modelRoleModuleGroup->where($where)->getField($field, $list);
    }
}